package com.fen.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.api.R;
import com.fen.common.Result;
import com.fen.common.StringConsts;
import com.fen.dao.UserMapper;
import com.fen.pojo.HeadPortrait;
import com.fen.pojo.User;
import com.fen.service.lmpl.HeadPortraitServicelmpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/**
 * @Description: springbootsc
 * @Author: lijiafen
 * @Date: 2021/12/15
 * @Time: 15:31
 * @eamil: 2368044269@qq.com
 */
@RestController
public class HeadPortraitController {
    @Autowired
    HeadPortraitServicelmpl servicelmpl;
    @Autowired
    UserMapper userMapper;
    /** 
    * @Description: 添加头像
    * @Author: lijiafen
    */ 
    @RequestMapping("/addLmg")
    public Result addLmg(Integer id,MultipartFile file)
{
         User user = userMapper.selectById(id);
        QueryWrapper<HeadPortrait>queryWrapper=new QueryWrapper<>();
        if (user==null)
        {
            return Result.getFailure().setDate(StringConsts.USER_NOT_EXIST);
        }
        if (file==null)
        {
            return Result.getFailure().setDate(StringConsts.IMG_FALL);
        }
       int i= servicelmpl.inlmg(id,file);
       if (i>0)
       {
           queryWrapper.eq("user_id",id);
            HeadPortrait one = servicelmpl.getOne(queryWrapper);
           return  Result.getSuccess().setDate(StringConsts.SAAE_OR_UPDATE_SUCCESS+one.getUrlImg());
       }
      return  Result.getFailure().setDate(StringConsts.SAVE_OR_UPDATE_FALL);
    }

    /**
    * @Description: 修改头像
    * @Author: lijiafen
    */
    @RequestMapping("/updateImg")
    public Result updateImg(Integer id,MultipartFile file)
    {
        if (id==null)
        {
            return Result.getFailure().setDate("未传用户参数");
        }
        if (file==null)
        {
            return Result.getFailure().setDate("未选择图片");
        }
        QueryWrapper<HeadPortrait>queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("user_id",id);
       HeadPortrait one = servicelmpl.getOne(queryWrapper);
       if (one==null)
       {
           System.out.println("");
           return Result.getFailure().setDate("找不到该用户");
       }
         Integer id1 = one.getId();
       //把id传给方法
        int i = servicelmpl.updateimg(id1, file);
        if (i>0)
        {
            HeadPortrait headPortrait = servicelmpl.getOne(queryWrapper);
            /*这里我们把修改后的图片地址传给前端让他们可以获取*/
            return Result.getSuccess().setDate(StringConsts.SAAE_OR_UPDATE_SUCCESS+headPortrait.getUrlImg());
        }else {
            return Result.getFailure().setDate(StringConsts.SAVE_OR_UPDATE_FALL);
        }
    }

}
